import { getListApi as getAllClientGradeApi } from "@/api/configCenter/clientGrade";
import { getSelectListApi as getAllAccountPeriodApi } from "@/api/configCenter/paymentManagement";
import { getAllCompanyApi, getUserByPositionApi } from "@/api/managementCenter/departmentPersonnel";
import CommonHook from "@/hook/commonHook";
import { reactive, ref } from "vue";

export default function InitHook() {
  const { dataDict } = CommonHook();
  const formDataRef = ref<any>(null);
  const formState = reactive<Partial<CustomerManagement.AddForm>>({
    shortName: undefined,
    companyId: undefined,
    followId: undefined,
    bizId: undefined,
    accountPeriodId: undefined,
    finId: undefined,
    levelId: undefined,
    status: 1,
    billingTimeType: "1",
    accountName: undefined,
    companyName: undefined,
    companyLegalPerson: undefined,
    // address: undefined,
    addressDetails: undefined,
    businessLicenseNumber: undefined,
    isRelease: undefined,
    businessLicenseUrlList: [],
    contactList: [
      {
        name: undefined,
        phone: undefined,
        address: undefined,
        email: undefined
      }
    ]
  });
  const companyOpts = ref<Array<Base.GeneraDataType>>([]);
  const followOpts = ref<Array<Base.GeneraDataType>>([]);
  const bizOpts = ref<Array<Base.GeneraDataType>>([]);
  const accountPeriodOpts = ref<Array<Base.GeneraDataType>>([]);
  const finOpts = ref<Array<Base.GeneraDataType>>([]);
  const customLevelOpts = ref<Array<Base.GeneraDataType>>([]);
  const loading = ref<boolean>(false);
  const getAllCompany = () => {
    return getAllCompanyApi({ status: 1, pageNum: 1, pageSize: 999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        companyOpts.value = data || [];
      }
    });
  };
  // 获取跟单
  const getAllFollow = () => {
    const { code } = dataDict.value["USER_POSITION"].find((t: any) => t.name === "跟单") || {};
    return getUserByPositionApi({ position: code, pageNum: 1, pageSize: 9999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        followOpts.value = data.records || [];
      }
    });
  };
  // 获取业务员
  const getAllBiz = () => {
    const { code } = dataDict.value["USER_POSITION"].find((t: any) => t.name === "业务员") || {};
    return getUserByPositionApi({ position: code, pageNum: 1, pageSize: 9999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        bizOpts.value = data.records || [];
      }
    });
  };
  // 获取财务
  const getAllFin = () => {
    const { code } = dataDict.value["USER_POSITION"].find((t: any) => t.name === "财务") || {};
    return getUserByPositionApi({ position: code, pageNum: 1, pageSize: 9999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        finOpts.value = data.records || [];
      }
    });
  };
  // 账期
  const getAllAccountPeriod = () => {
    return getAllAccountPeriodApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        accountPeriodOpts.value = data || [];
      }
    });
  };
  // 客户等级
  const getAllCustomerLevel = () => {
    return getAllClientGradeApi({ status: 1, pageNum: 1, pageSize: 999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        customLevelOpts.value = data.records || [];
      }
    });
  };
  const filter = (inputValue: string, path: Array<Base.GeneraDataType>) => {
    return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  };
  return {
    formDataRef,
    formState,
    companyOpts,
    getAllCompany,
    followOpts,
    getAllFollow,
    bizOpts,
    getAllBiz,
    dataDict,
    accountPeriodOpts,
    getAllAccountPeriod,
    getAllFin,
    finOpts,
    customLevelOpts,
    getAllCustomerLevel,
    filter,
    loading
  };
}
